package com.fdd.spring.proxy02;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;

//该类可以提供一个代理对象
public class VehicleProxyProvider {
    //定义一个属性，将来指向真实对象
    private Vehicle target_vehicle;
    public VehicleProxyProvider(Vehicle target_vehicle) {
        this.target_vehicle = target_vehicle;
    }


    //提供一个方法，返回一个代理对象
    public Object getProxy() {

        return (Vehicle) Proxy.newProxyInstance(target_vehicle.getClass().getClassLoader(),
                target_vehicle.getClass().getInterfaces(),
                new InvocationHandler() {
                    @Override
                    public Object invoke(Object proxy, java.lang.reflect.Method method, Object[] args) throws Throwable {
                        System.out.println("交通工具开始运行");
                        Object returnValue = method.invoke(target_vehicle, args);
                        System.out.println("交通工具结束运行");
                        return returnValue;
                    }
                });


    }
}